import { _decorator, Component, Label, Node, tween, v2, v3 } from 'cc';
import { UIBase } from '../../FrameWork3.8.6/FrameWork/UIFramework/UIBase';
import { PlayerPropertyBase } from './PlayerPropertyBase';
import { DataMgr } from '../../FrameWork3.8.6/FrameWork/Data/DataMgr';
import { EquipmentDtMgr, PlayerSkillDtMgr, ShiBingDtMgr } from '../../FrameWork3.8.6/FrameWork/Data/DataBaseMgr';
import { EquipmentDt, PlayerSkillDt, ShiBingDt } from '../../FrameWork3.8.6/FrameWork/Common/CommonInterface';
import { BagManager } from '../Game/Manager/BagManager';
import { GameManager } from '../Game/Manager/GameManager';
const { ccclass, property } = _decorator;

@ccclass('Menu')
export class Menu extends UIBase {
    //判断是否下拉，最开始可下拉
    private _isDown:boolean=true;
    onInit(...rest: any[]): void {
        //先注册下拉按钮事件
        this.addClickEventListener("_LaxiaBtn",this.pullDown);
        //注册装备事件，技能事件，兵种事件
        this.addClickEventListener("_EquipmentBtn",()=>{
            this.openUI("EquipmentPanel");
        })
        this.addClickEventListener("_SkillBtn",()=>{
            this.openUI("SkillPanel");
        })
        this.addClickEventListener("_ShiBingBtn",()=>{
            this.openUI("ShiBingPanel");
        })

        //注册关卡打开事件
        this.addClickEventListener("_LevelChangeBtn",()=>{
            this.openUI("LevelPanel")
        })

        //测试代码；
        if(GameManager.instance.isFirstEnter){
            //这里作测试用玩家发出拾取到技能
            let skillDtMgr=DataMgr.instance.getMgr<PlayerSkillDtMgr>("PlayerSkillDtMgr");
            // for(let i=1;i<=3;i++){
            let skillDt=skillDtMgr.getDataByID<PlayerSkillDt>(3001);
            BagManager.instance.addBagDt(skillDt.type,skillDt,false);
            BagManager.instance.addBagDt(skillDt.type,skillDt,true);
            // }
            let equipmentDtMgr=DataMgr.instance.getMgr<EquipmentDtMgr>("EquipmentDtMgr");
            for(let i=1;i<=6;i++){
                let equipmentDt=equipmentDtMgr.getDataByID<EquipmentDt>(8000+i);
                BagManager.instance.addBagDt(equipmentDt.type,equipmentDt,false);
            }
            let shiBingDtMgr=DataMgr.instance.getMgr<ShiBingDtMgr>("ShiBingDtMgr");
            for(let i=1;i<=3;i++){
                let shiBingDt=shiBingDtMgr.getDataByID<ShiBingDt>(2000+i);
                BagManager.instance.addBagDt(shiBingDt.type,shiBingDt,false);
                BagManager.instance.addBagDt(shiBingDt.type,shiBingDt,true);
            }


            GameManager.instance.isFirstEnter=false;
        }
        
    }

    onEnter(...rest: any[]): void {
        this.updateData();
    }

    //下拉事件回调函数
    pullDown(){
        //找到按钮父物体并且将其显示
        let node=this.getNode("_BtnParent");
        //当前为可下拉
        if(this._isDown){
            //先显示物体在缓动
            node.active=true;
            for(let i=0;i<node.children.length;i++){
                let child=node.children[i];
                tween(child)
                .by(0.3,{position:v3(0,-130*(i+1),0)})
                .start();
            }
            this._isDown=false;
            return;
            
        }
        //运行到这里为不可下拉
        //先缓动回去，再失活物体
        for(let child of node.children){
            tween(child)
            .to(0.3,{position:v3(0,0,0)})
            .start();
        }
        //node.active=false;
        this._isDown=true;
    }

    //每一次打开界面时都需要更新的界面信息
    updateData(){
        let coinLabel=this.getComp<Label>("_CoinLabel","Label");
        coinLabel.string=GameManager.instance.coinNum.toString();
    }
}


